package org.aplikator.client.shared.rpc.marshaller;

import org.aplikator.client.shared.descriptor.QueryParameter;
import org.jboss.errai.marshalling.client.api.MarshallingSession;
import org.jboss.errai.marshalling.client.api.annotations.ClientMarshaller;
import org.jboss.errai.marshalling.client.api.annotations.ServerMarshaller;
import org.jboss.errai.marshalling.client.api.json.EJObject;
import org.jboss.errai.marshalling.client.api.json.EJValue;
import org.jboss.errai.marshalling.client.marshallers.AbstractNullableMarshaller;

@ClientMarshaller(QueryParameter.class)
@ServerMarshaller(QueryParameter.class)
public class QueryParameterMarshaller extends AbstractNullableMarshaller<QueryParameter> {

	/*@Override
	public Class<QueryParameter> getTypeHandled() {
		return QueryParameter.class;
	}*/

	@Override
	public QueryParameter[] getEmptyArray() {
		return new QueryParameter[0];
	}

	@Override
	public QueryParameter doNotNullDemarshall(EJValue o, MarshallingSession ctx) {
		if (o.isObject() != null) {
			EJObject ejObject = o.isObject();
			String name = DemarshallingUtils.safeString(ejObject,"name");
			String value =  DemarshallingUtils.safeString(ejObject,"value");
			QueryParameter qp = new QueryParameter(name);
			qp.setValue(value);
			return qp;
		} else return null;
	}

	@Override
	public String doNotNullMarshall(QueryParameter o, MarshallingSession ctx) {
		StringBuilder builder = new StringBuilder().append('{');
		MarshallingUtils.string("name", o.getName(), builder).append(',');
		MarshallingUtils.string("value", o.getValue(), builder);
		builder.append('}');
		return builder.toString();
	}
}
